package com.cqc.volleydemo1.net;

import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.cqc.volleydemo1.application.MyApplication;

import java.util.Map;

/**
 * Created by ${cqc} on 2016/9/26.
 */

public class MainRequest {

    private static final String TAG = "MainRequest";
    private static MainRequest mainRequest;

    private MainRequest() {

    }

    public static MainRequest getInstance() {
        if (mainRequest == null) {
            mainRequest = new MainRequest();
        }
        return mainRequest;
    }

    //post请求
    public void makePostRequest(String url, final Map<String, String> map, RequestTag requestTag) {
        StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d(TAG, "response=" + response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d(TAG, "error=" + error.getMessage());
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return map;
            }
        };
        request.setTag(requestTag);
        MyApplication.getRequestQueue().add(request);
    }

    //get请求,失败：不要用
    public void makeGetRequest(String url, final Map<String, String> map, RequestTag requestTag) {
        StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d(TAG, "response=" + response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d(TAG, "error=" + error.getMessage());
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return map;
            }
        };
        request.setTag(requestTag);
        MyApplication.getRequestQueue().add(request);
    }

    //get请求，url是拼接的，把参数拼进去。
    public void makeGetRequest(String url, RequestTag requestTag) {
        StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d(TAG, "response=" + response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d(TAG, "error=" + error.getMessage());
            }
        });
        request.setTag(requestTag);
        MyApplication.getRequestQueue().add(request);
    }
}
